<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class DeliveryOrderModel
{

    public static function lists($param): array
    {
        // $userIds = getScopeView($param['uid']);
        // if ($userIds != 0) {
            // $where[] = ['uid', 'in', $userIds];
        // }
        // $where[] = [
        //     'partner_id', '=', $param['partner_id'],
        // ];

        $where[] = [1, '=', 1];

        if (isset($param['delivery_no']) && !empty($param['delivery_no'])) {
            $where[] = ['do.delivery_no', '=', $param['delivery_no']];
        }

        if (isset($param['car_id']) && !empty($param['car_id'])) {
            $where[] = ['do.car_id', '=', $param['car_id']];
        }

        if (isset($param['network_ids'])&& !empty($param['network_ids'])) {
            $where[] = ['do.network_id', 'IN', $param['network_ids']];
        }

        if (isset($param['datestr'])&& !empty($param['datestr'])) {
            $where[] = ['do.datestr', '=', $param['datestr']];
        }

        if (isset($param['delivery_status'])&& !empty($param['delivery_status'])) {
            $where[] = ['do.delivery_status', '=', $param['delivery_status']];
        }
        
        if (isset($param['task_id']) && !empty($param['task_id'])) {
            $where[] = ['do.tms_task_id', '=', $param['task_id']];
        }

        if (isset($param['stime']) && isset($param['etime']) && !empty($param['stime']) && !empty($param['etime'])) {
            $where[] = ['do.add_time', 'between', [$param['stime'], $param['etime']]];
        }

        if (isset($param['net_name']) && !empty($param['net_name'])) {
            $where[] = ['net.name', 'like', '%' . $param['net_name'] . '%'];
        }

        if (isset($param['driver_name']) && !empty($param['driver_name'])) {
            $where[] = ['d.name', 'like', '%' . $param['driver_name'] . '%'];
        }

        if (isset($param['car_no']) && !empty($param['car_no'])) {
            $where[] = ['c.car_plate_number', 'like', '%' . $param['car_no'] . '%'];
        }

        if (isset($param['is_export']) && $param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_delivery_order')->alias('do')
            ->join('xl_networks net', 'net.id = do.network_id', 'left')
            ->join('xl_driver d', 'd.id = do.driver_id', 'left')
            ->join('xl_cars c', 'c.id = do.car_id', 'left')
            ->where($where)->count();
        }
        $lists = Db::table('xl_delivery_order')->alias('do')
            ->join('xl_networks net', 'net.id = do.network_id', 'left')
            ->join('xl_driver d', 'd.id = do.driver_id', 'left')
            ->join('xl_cars c', 'c.id = do.car_id', 'left')
            ->where("do.status", "<", 3)->where([$where])
            ->order('do.add_time desc')
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->field('do.*, net.name, d.name as driver_name, c.car_plate_number as car_no')
            ->select()->toArray();

        $count = Db::table('xl_delivery_order')->alias('do')
            ->join('xl_networks net', 'net.id = do.network_id', 'left')
            ->join('xl_driver d', 'd.id = do.driver_id', 'left')
            ->join('xl_cars c', 'c.id = do.car_id', 'left')
            ->where("do.status", "<", 3)
            ->where([$where])
            ->count();
            
        return ['lists' => $lists, 'count' => $count];
    }


    public static function info($id)
    {
        return Db::table('xl_delivery_order')->where('id', '=', $id)->field('id,oms_order_ids,link_order_ids')->find();
    }
}